<?php

use yii\helpers\Url;

$this->context->layout = false;
?>
<?php
//使用率
echo $this->render("@app/views/public/useRate.php")
?>
<div id="History-Record-List"></div>
<script type="text/x-kendo-template" id="historyRecordAuthTemplate">
    <button type="button" data-command="create" class="k-button k-button-icontext k-grid-add"><span
            class="k-icon k-add"></span>新增
    </button>
    <a class="k-button k-button-icontext" onclick="onButtonAuth()"><span class="glyphicon glyphicon-cog"></span>部门类别</a>
    <a class="k-button k-button-icontext " onclick="onLeaderAuth()"><span
            class="glyphicon glyphicon-cog"></span>代理组长</a>
    <a class="k-button k-button-icontext" onclick="onNotCommented()"><span class="glyphicon glyphicon-cog"></span>未评五大类</a>
    <a class="k-button k-button-icontext btn btn-info btn-lg active"><span class="glyphicon glyphicon-cog"></span>查看历史</a>
    <a class="k-button k-button-icontext" onclick="onSummaryMaintain()"><span class="glyphicon glyphicon-cog"></span>查看总汇</a>
    <a class="k-button k-button-icontext" onclick="onSummaryManagement()"><span class="glyphicon glyphicon-cog"></span>总汇管理层</a>
    <a class="k-button k-button-icontext" onclick="onRatingSettings()"><span class="glyphicon glyphicon-cog"></span>评级设置</a>
    <a class="k-button k-button-icontext" onclick="onAuthManagement()"><span class="glyphicon glyphicon-cog"></span>权限管理</a>
    <a class="k-button k-button-icontext" onclick="onAffectPrincipal()"><span class="glyphicon glyphicon-cog"></span>被影響人負責人</a>
</script>
<script>
    var departments;
    $.ajax({
        url: "<?=Url::to(['getgroupdepartment']) ?>",
        dataType: "json",
        success: function (redata) {
            departments = redata;
        }
    });
    var historyRecord_grid = $("#History-Record-List");
    $(document).ready(function () {
        historyRecord_grid.kendoGrid({
            dataSource: {
                transport: {
                    read: function (e) {
                        $.ajax({
                            url: "<?=Url::to(['historyrecordlist']); ?>",
                            dataType: "json",
                            success: function (redata) {
                                e.success(redata);
                            },error:function (e) {
                                layer.msg('网络错误！', {icon: 5});
                            }
                        })
                    },
                    create: function (e) {
                        $.ajax({
                            url: "<?=Url::to(['createhistoryrecord']); ?>",
                            dataType: "json",
                            type: "POST",
                            data: e.data,
                            success: function (redata) {
                                if (redata.status == 1) {
                                    refresh();
                                } else {
                                    layer.msg(redata.msg, {icon: 5});
                                }
                            },error:function (e) {
                                layer.msg('网络错误！', {icon: 5});
                            }
                        })
                    },
                    update: function (e) {
                        $.ajax({
                            url: "<?=Url::to(['updatehistoryrecord'])?>",
                            dataType: "json",
                            type: "POST",
                            data: e.data,
                            success: function (redata) {
                                if (redata.status == 1) {
                                    refresh();
                                } else {
                                    layer.msg(redata.msg, {icon: 5});
                                }
                            },error:function (e) {
                                layer.msg('网络错误！', {icon: 5});
                            }
                        })
                    },
                    destroy: function (e) {
                        $.ajax({
                            url: "<?=Url::to(['destroyhistoryrecord'])?>",
                            dataType: "json",
                            type:"POST",
                            data: {id: e.data.id},
                            success: function (redata) {
                                if (redata.status != 1) {
                                    layer.msg(redata.msg, {icon: 5});
                                }
                                refresh();
                            },error:function (e) {
                                layer.msg('网络错误！', {icon: 5});
                            }
                        })
                    }
                },
                schema: {
                    model: {
                        id: "id",
                        fields: {
                            id: {field: "id", editable: false, nullable: true},
                            username: {field: "username"},
                            cn_name: {field: "cn_name", editable: false},
                            departments: {field: "departments"}
                        }
                    }
                },
                pageSize: 15
            },
            filterable: {
                extra: false,
                operators: {
                    string: {
                        contains: "包含",
                        doesnotcontain: "不包含",
                        eq: "等于",
                        neq: "不等于"
                    }
                }
            },
            sortable: true,
            toolbar: kendo.template($("#historyRecordAuthTemplate").html()),
            editable: "popup",
            pageable: {
                refresh: true,
                pageSizes: [15, 30, 45, "all"],
                buttonCount: 5
            },
            columns: [
                {
                    field: "cn_name",
                    title: "所属部门",
                    width: "15%",
                    headerAttributes: alignCenterAttribute,
                    attributes: alignCenterAttribute
                },
                {
                    field: "username",
                    title: "用户",
                    editor: userEdit,
                    width: "15%",
                    headerAttributes: alignCenterAttribute,
                    attributes: alignCenterAttribute
                },
                {
                    field: "departments",
                    title: "可查看部门",
                    editor: selectDepartment,
                    template: function (e) {
                        var departmentNames = new Array();
                        for (var o in departments) {
                            for (var b = 0; b < e.departments.length; b++) {
                                if (departments[o].id == e.departments[b]) {
                                    departmentNames.push(departments[o].name);
                                }
                            }
                        }
                        return departmentNames;
                    }
                },
                {
                    title: "操作",
                    width: "15%",
                    command:
                        [
                            "edit",
                            "destroy"
                        ]
                }
            ]
        })
        ;
    });

    function selectDepartment(container, options) {
        var multiselect = $('<select required id="selectDepartment" data-text-field="name" data-value-field="id" data-bind="value:' + options.field + '"></select>');
        multiselect.appendTo(container)
            .kendoMultiSelect({
                dataTextField: "name",
                dataValueField: "id",
                filter: "contains",
                dataSource:{
                    data:departments,
                    group: { field: "group" }
                }
            });
    }
    function refresh() {
        var _grid = historyRecord_grid.data("kendoGrid");
        _grid.dataSource.read();
        _grid.refresh();
    }

    function userEdit(container, options) {
        if (typeof options.model.id == 'string') {
            $('<div>'+options.model.username+'</div>').appendTo(container);
            return ;
        }
        $('<select required data-text-field="name" data-value-field="id" data-bind="value:' + options.field + '"></select>')
            .appendTo(container)
            .kendoDropDownList({
                dataTextField: "name",
                dataValueField: "id",
                filter: "contains",
                dataSource: {
                    transport: {
                        read: function (e) {
                            $.ajax({
                                url: "<?=Url::to(['getstaffs']) ?>",
                                dataType: "json",
                                success: function (redata) {
                                    e.success(redata)
                                }
                            });
                        }
                    }
                }
            });
    }
</script>
